﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace myFirtz.Interface
{
    [XmlType("device")]
    public class Device
    {
        #region xml Elemets
        private string _identifier;
        [XmlAttribute("identifier")]
        public string Identifier
        {
            get
            {
                return (_identifier);
            }
            set
            {
                _identifier = value;
            }
        }

        private string _id;
        [XmlAttribute("id")]
        public string Id
        {
            get
            {
                return (_id);
            }
            set
            {
                _id = value;
            }
        }

        private string _fwversion;
        [XmlAttribute("fwversion")]
        public string fwversion
        {
            get
            {
                return (_fwversion);
            }
            set
            {
                _fwversion = value;
            }
        }

        private string _functionbitmask;
        [XmlAttribute("functionbitmask")]
        public string Functionbitmask
        {
            get
            {
                return (_functionbitmask);
            }
            set
            {
                _functionbitmask = value;
            }
        }

        private string _manufacturer;
        [XmlAttribute("manufacturer")]
        public string Manufacturer
        {
            get
            {
                return (_manufacturer);
            }
            set
            {
                _manufacturer = value;
            }
        }

        private string _productname;
        [XmlAttribute("productname")]
        public string Productname
        {
            get
            {
                return (_productname);
            }
            set
            {
                _productname = value;
            }
        }
        private string _present;
        [XmlElement("present")]
        public string Present
        {
            get
            {
                return (_present);
            }
            set
            {
                _present = value;
            }
        }

        private string _name;
        [XmlElement("name")]
        public string Name
        {
            get
            {
                return (_name);
            }
            set
            {
                _name = value;
            }
        }

        private Switch _switch;
        [XmlElement("switch")]
        public Switch Switch
        {
            get
            {
                return (_switch);
            }
            set
            {
                _switch = value;
            }
        }

        private Powermeter _powermeter;
        [XmlElement("powermeter")]
        public Powermeter Powermeter
        {
            get
            {
                return (_powermeter);
            }
            set
            {
                _powermeter = value;
            }
        }

        private Temperature _temperature;
        [XmlElement("temperature")]
        public Temperature Temperature
        {
            get
            {
                return (_temperature);
            }
            set
            {
                _temperature = value;
            }
        }

        private HKR _hkr;
        [XmlElement("hkr")]
        public HKR HKR
        {
            get
            {
                return (_hkr);
            }
            set
            {
                _hkr = value;
            }
        }

        #endregion

        #region HTML Markup

        public string ConnectionStatus
        {
            get
            {
                if (_present == "1")
                {
                    return ("Verbunden");
                }
                else
                {
                    return ("Getrennt");
                }
            }
        }

        public string HasTemperature
        {
            get
            {
                if (Temperature == null)
                {
                    return (String.Format("hidden = {0}hidden{0}", '"'));
                }
                return (string.Empty);
            }
        }

        public string HasHKR
        {
            get
            {
                if (HKR == null)
                {
                    return (String.Format("hidden = {0}hidden{0}", '"'));
                }
                return (string.Empty);
            }
        }

        public string IsChecked
        {
            get
            {
                if (_switch != null)
                {
                    if (_switch.State == "1")
                        return ("An");
                }
                return ("Aus");
            }
        }

        public string GetBtnCss
        {
            get
            {
                if (_switch != null)
                {
                    if (_switch.State == "1")
                        return ("OnBtn");
                }
                return ("OffBtn");
            }
        }

        #endregion
    }

    [XmlType("switch")]
    public class Switch
    {
        private string _state;
        [XmlElement("state")]
        public string State
        {
            get
            {
                return (_state);
            }
            set
            {
                _state = value;
            }
        }

        private string _mode;
        [XmlElement("mode")]
        public string Mode
        {
            get
            {
                return (_mode);
            }
            set
            {
                _mode = value;
            }
        }

        private string _lock;
        [XmlElement("lock")]
        public string Lock
        {
            get
            {
                return (_lock);
            }
            set
            {
                _lock = value;
            }
        }
    }

    [XmlType("powermeter")]
    public class Powermeter
    {
        private string _power;
        [XmlElement("power")]
        public string Power
        {
            get
            {
                if (string.IsNullOrEmpty(_power))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_power);
                    return ((dec / 10).ToString());
                }
            }
            set
            {
                _power = value;
            }
        }

        private string _energy;
        [XmlElement("energy")]
        public string Energy
        {
            get
            {
                if (string.IsNullOrEmpty(_energy))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_energy);
                    return ((dec / 10).ToString());
                }
            }
            set
            {
                _energy = value;
            }
        }
    }

    [XmlType("temperature")]
    public class Temperature
    {
        private string _celsius;
        [XmlElement("celsius")]
        public string Celsius
        {
            get
            {
                if (string.IsNullOrEmpty(_celsius))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_celsius);
                    return ((dec / 10).ToString());

                }
            }
            set
            {
                _celsius = value;
            }
        }

        private string _offset;
        [XmlElement("offset")]
        public string Offset
        {
            get
            {
                return (_offset);
            }
            set
            {
                _offset = value;
            }
        }
    }

    [XmlType("hkr")]
    public class HKR
    {
        private string _tist;
        [XmlElement("tist")]
        public string Tist
        {
            get
            {
                if (string.IsNullOrEmpty(_tist))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_tist);
                    return ((dec / 10).ToString());

                }
            }
            set
            {
                _tist = value;
            }
        }

        private string _tsoll;
        [XmlElement("tsoll")]
        public string Tsoll
        {
            get
            {
                if (string.IsNullOrEmpty(_tsoll))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_tsoll);
                    return ((dec / 10).ToString());

                }
            }
            set
            {
                _tsoll = value;
            }
        }

        private string _komfort;
        [XmlElement("komfort")]
        public string Komfort
        {
            get
            {
                if (string.IsNullOrEmpty(_komfort))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_komfort);
                    return ((dec / 10).ToString());

                }
            }
            set
            {
                _komfort = value;
            }
        }

        private string _absenk;
        [XmlElement("absenk")]
        public string Absenk
        {
            get
            {
                if (string.IsNullOrEmpty(_absenk))
                {
                    return ("0");
                }
                else
                {
                    decimal dec = Convert.ToDecimal(_absenk);
                    return ((dec / 10).ToString());

                }
            }
            set
            {
                _absenk = value;
            }
        }
    }

    [Serializable, XmlRoot("devicelist"), XmlType("devicelist")]
    public class DeviceList
    {
        private List<Device> _devicelist;
        [XmlElement("device")]
        public List<Device> devicelist
        {
            get
            {
                return (_devicelist);
            }
            set
            {
                _devicelist = value;
            }
        }

        public DeviceList()
        {
            _devicelist = new List<Device>();
        }

        public void LoadExample()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(DeviceList));
            FileStream fs = new FileStream(@"C:\Temp\xxx.xml", FileMode.Open);
            DeviceList i = (DeviceList)serializer.Deserialize(fs);
            this._devicelist = i.devicelist;
            fs.Close();
        }
    }
}